// api/ai_role_api/ai_role_work_order_list.go
package ai_role_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/service/common"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

type AiRoleWorkOrderListRequest struct {
	models.Page
	UserID uint `json:"userID" form:"userID"`
	Type   int8 `json:"type" form:"type"`
	Status int8 `json:"status" form:"status"`
}

type AiRoleWorkOrderListResponse struct {
	models.AiRoleWorkOrderModel
	AiRoleTitle  string `json:"aiRoleTitle"`
	AiRoleAvatar string `json:"aiRoleAvatar"`
	UserTitle    string `json:"userTitle"`
	UserAvatar   string `json:"userAvatar"`
}

func (AiRoleApi) AiRoleWorkOrderListView(c *gin.Context) {
	var cr AiRoleWorkOrderListRequest
	c.ShouldBindQuery(&cr)

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	query := global.DB.Where("")
	if claims.Role == ctype.AuthRole {
		query.Where("user_id = ?", claims.UserID)
	}

	_list, count, _ := common.QueryList(models.AiRoleWorkOrderModel{
		UserID: cr.UserID,
		Type:   cr.Type,
		Status: cr.Status,
	}, common.Option{
		Page:     cr.Page,
		Preloads: []string{"UserModel", "AiRoleModel"},
		Where:    query,
		Sort:     "created_at desc",
	})
	var list = make([]AiRoleWorkOrderListResponse, 0)
	for _, model := range _list {
		list = append(list, AiRoleWorkOrderListResponse{
			AiRoleWorkOrderModel: model,
			AiRoleTitle:          model.AiRoleModel.Title,
			AiRoleAvatar:         model.AiRoleModel.Avatar,
			UserTitle:            model.UserModel.Nickname,
			UserAvatar:           model.UserModel.Avatar,
		})
	}

	res.OkWithList(c, list, count)
}
